<template>
  <ConfigForm rawKey="department_id" :server="departmentServer" :configs="createForm" :beforeSubmit="beforeSubmit" />
</template>

<script setup lang="jsx">
import ConfigForm from '@/components/CRUD/ConfigForm.vue'
import { departmentServer } from '@/api'
import OrganizationDialog from '@/components/Organization/OrganizationDialog.vue'

const createForm = [
  {
    label: '上级部门',
    key: 'parent',
    type: 'custom',
    required: true,
    props: {
      type: 'department',
      placeholder: '请选择上级部门',
    },
    render: OrganizationDialog,
  },
  {
    label: '名称',
    key: 'name',
    type: 'input',
    required: true,
    props: {
      placeholder: '请输入部门名称',
    },
  },
  {
    label: '负责人',
    key: 'leader',
    type: 'custom',
    required: true,
    props: {
      type: 'user',
      placeholder: '请输入负责人',
    },
    render: OrganizationDialog,
  },
]

// 提交表单前的处理
const beforeSubmit = (params) => {
  // 处理上级部门的值
  if (params.parent) {
    params.pid = params.parent.department_id
    delete params.parent
  }
  // 处理负责人
  if (params.leader) {
    params.leader_id = params.leader.user_id
    delete params.leader
  }
  return params
}
</script>